package main

import (
	"github.com/nsf/termbox-go"
)

type Cell struct {
	x      int
	y      int
	Fg     termbox.Attribute
	Bg     termbox.Attribute
	occupy bool
}

// 方块
type block struct {
	typeId [2]int
	cell   [8]Cell
}

func (e *engine) left() {
	minLeft := e.w
	for _, v := range e.nowGraphics.cell {
		if v.x < minLeft {
			minLeft = v.x
		}
	}
	if minLeft <= 0 {
		return
	}
	for k, v := range e.nowGraphics.cell {
		e.globalMap[v.y][v.x] = Cell{}
		e.nowGraphics.cell[k] = Cell{
			x:      v.x - 2,
			y:      v.y,
			Fg:     v.Fg,
			Bg:     v.Bg,
			occupy: v.occupy,
		}
	}
}
func (e *engine) right() {
	maxLeft := 0
	for _, v := range e.nowGraphics.cell {
		if v.x > maxLeft {
			maxLeft = v.x
		}
	}
	if maxLeft >= e.w-1 {
		return
	}
	for k, v := range e.nowGraphics.cell {
		e.globalMap[v.y][v.x] = Cell{}
		e.nowGraphics.cell[k] = Cell{
			x:      v.x + 2,
			y:      v.y,
			Fg:     v.Fg,
			Bg:     v.Bg,
			occupy: v.occupy,
		}
	}
}
func (e *engine) up() {
	e.shape(e.nowGraphics)
}
func (e *engine) down() {
	for k, v := range e.nowGraphics.cell {
		e.globalMap[v.y][v.x] = Cell{}
		e.nowGraphics.cell[k] = Cell{
			x:      v.x,
			y:      v.y + 1,
			Fg:     v.Fg,
			Bg:     v.Bg,
			occupy: v.occupy,
		}
	}
}

func (e *engine) round(now [2]int) {

}

// 获取一个图形
func (e *engine) createBlock() {
	b := random(len(blockArr) - 1)
	t := random(len(blockArr[b]) - 1)
	tu := block{
		typeId: [2]int{0, t},
	}
	e.nowGraphics.typeId = tu.typeId
	color := getPieceColor()
	switch tu.typeId[0] {
	case 0: //正方体
		e.nowGraphics.cell = [8]Cell{
			{
				x:      e.w/2 - 2,
				y:      0,
				Fg:     fgColor,
				Bg:     color,
				occupy: true,
			},
			{
				x:      e.w/2 - 1,
				y:      0,
				Fg:     fgColor,
				Bg:     color,
				occupy: true,
			},
			{
				x:      e.w / 2,
				y:      0,
				Fg:     fgColor,
				Bg:     color,
				occupy: true,
			},
			{
				x:      e.w/2 + 1,
				y:      0,
				Fg:     fgColor,
				Bg:     color,
				occupy: true,
			},
			{
				x:      e.w/2 - 2,
				y:      1,
				Fg:     fgColor,
				Bg:     color,
				occupy: true,
			},
			{
				x:      e.w/2 - 1,
				y:      1,
				Fg:     fgColor,
				Bg:     color,
				occupy: true,
			},
			{
				x:      e.w / 2,
				y:      1,
				Fg:     fgColor,
				Bg:     color,
				occupy: true,
			},
			{
				x:      e.w/2 + 1,
				y:      1,
				Fg:     fgColor,
				Bg:     color,
				occupy: true,
			},
		}
	case 1: //长条竖
	case 2: //s横
	case 3: //z横
	case 4: //l横
	case 5: //J
	case 6: //T:
	}
}

//生成图形

// 变形
func (e *engine) shape(nowBlock block) block {
	return block{}
}
